package com.ibm.law.ui.system.setup;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;

import com.ibm.law.domain.StaffList;
import com.ibm.law.service.system.StaffService;
import com.ibm.law.ui.common.CloseDialogSelectionAdapter;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.BeansObservables;
import com.ibm.law.domain.Staff;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.viewers.CellEditorProperties;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.IBeanValueProperty;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.jface.viewers.ComboBoxCellEditor;

public class UserRightSetup extends Dialog {
	private DataBindingContext m_bindingContext;

	protected Object result;
	protected Shell shell;
	private Table table;
	private Button btnNewButton;
	private Button btnNewButton_1;
	private Button btnNewButton_2;
	private Button btnNewButton_3;
	private TableColumn pwdColumn;
	private TableViewerColumn tvcPwd;
	private StaffList staffList=new StaffList();
	private StaffService staffService;
	private TableViewer tableViewer;
	private TableColumn tblclmnNewColumn_3;
	private TableColumn enabledColumn;
	private TableViewerColumn tvcEnabled;
	private TableColumn userNoColumn;
	private TableViewerColumn tvcUserNo;
	private TableColumn usernameColumn;
	private TableViewerColumn tvcUsername;
	private TableColumn deptColumn;
	private TableViewerColumn tvcDept;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public UserRightSetup(Shell parent, int style) {
		super(parent, style);
		staffService = new StaffService();
		staffList.setList(staffService.getAll());
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(800, 600);
		shell.setText("用户权限设置");
		shell.setLayout(new GridLayout(2, false));
		
		tableViewer = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		tvcUserNo = new TableViewerColumn(tableViewer, SWT.NONE);
		userNoColumn = tvcUserNo.getColumn();
		userNoColumn.setWidth(100);
		userNoColumn.setText("用户号");
		
		tvcUsername = new TableViewerColumn(tableViewer, SWT.NONE);
		usernameColumn = tvcUsername.getColumn();
		usernameColumn.setWidth(100);
		usernameColumn.setText("用户名");
		
		tvcDept = new TableViewerColumn(tableViewer, SWT.NONE);
		deptColumn = tvcDept.getColumn();
		deptColumn.setWidth(100);
		deptColumn.setText("部门");
		
		tvcPwd = new TableViewerColumn(tableViewer, SWT.NONE);
		pwdColumn = tvcPwd.getColumn();
		pwdColumn.setWidth(100);
		pwdColumn.setText("密码");
		
		tvcEnabled = new TableViewerColumn(tableViewer, SWT.NONE);
		enabledColumn = tvcEnabled.getColumn();
		enabledColumn.setWidth(100);
		enabledColumn.setText("停用标志");
		enabledColumn.setWidth(100);
		enabledColumn.setText("停用标记");
		
		CheckboxTreeViewer checkboxTreeViewer = new CheckboxTreeViewer(shell, SWT.BORDER);
		Tree tree = checkboxTreeViewer.getTree();
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Staff staff=new Staff();
				staffList.add(staff);
				tvcPwd.getViewer().editElement(staff, 0);
			}
		});
		btnNewButton.setText("增加");
		
		btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selection=(IStructuredSelection)tableViewer.getSelection();
				if(selection!=null){
					staffService.delete((Staff)selection.getFirstElement());
				}
			}
		});
		btnNewButton_1.setText("删除");
		
		btnNewButton_2 = new Button(composite, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				staffService.saveAll(staffList.getList());
			}
		});
		btnNewButton_2.setText("保存");
		
		btnNewButton_3 = new Button(composite, SWT.NONE);
		btnNewButton_3.addSelectionListener(new CloseDialogSelectionAdapter());
		btnNewButton_3.setText("退出");
		m_bindingContext = initDataBindings();

	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider.getKnownElements(), Staff.class, new String[]{"userNo", "username", "department", "password", "enabled"});
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		tableViewer.setContentProvider(listContentProvider);
		//
		IObservableList listStaffListObserveList = BeanProperties.list("list").observe(staffList);
		tableViewer.setInput(listStaffListObserveList);
		//
		CellEditor cellEditor = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty = BeanProperties.value("userNo");
		tvcUserNo.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor, cellEditorProperty, valueProperty));
		//
		CellEditor cellEditor_1 = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty_1 = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty_1 = BeanProperties.value("username");
		tvcUsername.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor_1, cellEditorProperty_1, valueProperty_1));
		//
		CellEditor cellEditor_2 = new ComboBoxCellEditor(tableViewer.getTable(), new String[0]);
		IValueProperty cellEditorProperty_2 = CellEditorProperties.control().value(WidgetProperties.selection());
		IBeanValueProperty valueProperty_2 = BeanProperties.value("department");
		tvcDept.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor_2, cellEditorProperty_2, valueProperty_2));
		//
		CellEditor cellEditor_3 = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty_3 = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty_3 = BeanProperties.value("password");
		cellEditor_3.setStyle(SWT.PASSWORD);
		tvcPwd.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor_3, cellEditorProperty_3, valueProperty_3));
		//
		return bindingContext;
	}
}
